package com.maykk.Demo04;

import java.sql.*;

/**
 * @ClassName: JdbcUtilsSing
 * @Description: TODO Jdbc 单例模式 工具类
 * @Author: MaykkStar@163.com
 */
public class JdbcUtilsSing {
    private static final String URL = "jdbc:mysql://localhost:3306/maykk?useSSL=false";
    private static final String USER = "root";
    private static final String PWD = "123456";

    private static JdbcUtilsSing instance = null;

    private JdbcUtilsSing() {}

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static JdbcUtilsSing getInstance() {
        if (null == instance) {
            instance = new JdbcUtilsSing();
        }
        return instance;
    }

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL,USER,PWD);
    }

    public void free(ResultSet rs, Statement st, Connection conn) {
        try {
            if (null != rs) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != st) {
                    st.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != conn) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
